<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item prop="type" label="类型" size="small">
            <el-select v-model="searchForm.type" placeholder="请选择">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="合同名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="singleDatePicker">
          <el-form-item prop="signDate" label="签订日期" size="small" class="item-inline">
            <el-date-picker
              v-model="searchForm.signDate"
              type="year"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="签订单位" size="small">
            <el-input v-model="searchForm.signUnit"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="80"
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.projectId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="600px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
        <el-form-item prop="type" label="类型" size="small">
          <el-select v-model="teamRow.type" placeholder="请选择" :disabled="dialogType!==1">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同编号" size="small">
          <el-input type="text" v-model="teamRow.projectContract"></el-input>
        </el-form-item>
        <el-form-item prop="projectName" label="合同名称" size="small">
          <el-input type="text" v-model="teamRow.projectName"></el-input>
        </el-form-item>
        <el-form-item prop="signDate" label="签订日期" size="small" class="item-inline">
          <el-date-picker
            v-model="teamRow.signDate"
            type="date"
            value-format="timestamp"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="signUnit" label="签订单位" size="small">
          <el-input type="text" v-model="teamRow.signUnit"></el-input>
        </el-form-item>
        <el-form-item label="合同金额" size="small">
          <el-input type="number" v-model.number="teamRow.projectFee">
            <span slot="suffix">万元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="付款情况" size="small">
        </el-form-item>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第一次日期" size="small">
              <el-date-picker
                v-model="teamRow.oneDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.oneFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第二次日期" size="small">
              <el-date-picker
                v-model="teamRow.twoDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.twoFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第三次日期" size="small">
              <el-date-picker
                v-model="teamRow.threeDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.threeFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第四次日期" size="small">
              <el-date-picker
                v-model="teamRow.fourDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.fourFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第五次日期" size="small" class="item-inline">
              <el-date-picker
                v-model="teamRow.fiveDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.fiveFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col class="singleDatePicker">
            <el-form-item label="第六次日期" size="small" class="item-inline">
              <el-date-picker
                v-model="teamRow.sixDate"
                type="date"
                value-format="timestamp"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="金额" size="small">
              <el-input type="number" v-model.number="teamRow.sixFee">
                <span slot="suffix">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="invoiceName" label="开票名称" size="small">
          <el-input type="text" v-model="teamRow.invoiceName"></el-input>
        </el-form-item>
        <el-form-item prop="ird" label="税号" size="small">
          <el-input type="text" v-model="teamRow.ird"></el-input>
        </el-form-item>
        <el-form-item label="开票地址" size="small">
          <el-input type="text" v-model="teamRow.address"></el-input>
        </el-form-item>
        <el-form-item label="电话" size="small">
          <el-input type="text" v-model="teamRow.tel"></el-input>
        </el-form-item>
        <el-form-item label="开户行" size="small">
          <el-input type="text" v-model="teamRow.openingBank"></el-input>
        </el-form-item>
        <el-form-item label="开户账号" size="small">
          <el-input type="text" v-model="teamRow.account"></el-input>
        </el-form-item>
        <el-form-item label="备注说明" size="small">
          <el-input type="textarea" v-model="teamRow.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    updateContractDeal,
    addContractDeal,
    delContractDeal,
    selectContractDeal
  } from '@/api/contractdeal'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const configs = {
    props: ['colConfig'],
    filters: {
      formatMonth(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMM')
      },
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyyMMdd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='month'"> {{ scope.row[item.prop] | formatMonth }}</span>
                    <span v-else-if="item.component==='time'"> {{ scope.row[item.prop] | formatDate }}</span>
                    <span v-else >{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  const type = function(row, column) {
    return row.type === 1 ? '付款' : '到账'
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'projectContract', label: '合同编号', width: '100', fixed: true },
          { prop: 'projectName', label: '合同名称', width: '150', fixed: true },
          { prop: 'type', label: '类型', width: '60', fixed: true, formatter: type },
          {
            label: '合同情况', children: [
              { prop: 'signDate', label: '签订日期', width: '100', component: 'time' },
              { prop: 'signUnit', label: '签订单位', width: '150' },
              { prop: 'projectFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第一次', children: [
              { prop: 'oneDate', label: '日期', width: '100', component: 'time' },
              { prop: 'oneFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第二次', children: [
              { prop: 'twoDate', label: '日期', width: '100', component: 'time' },
              { prop: 'twoFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第三次', children: [
              { prop: 'threeDate', label: '日期', width: '100', component: 'time' },
              { prop: 'threeFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第四次', children: [
              { prop: 'fourDate', label: '日期', width: '100', component: 'time' },
              { prop: 'fourFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第五次', children: [
              { prop: 'fiveDate', label: '日期', width: '100', component: 'time' },
              { prop: 'fiveFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '第六次', children: [
              { prop: 'sixDate', label: '日期', width: '100', component: 'time' },
              { prop: 'sixFee', label: '金额(万元)', width: '80' }
            ], component: configs
          },
          {
            label: '开票信息', children: [
              { prop: 'invoiceName', label: '名称', width: '150' },
              { prop: 'ird', label: '税号', width: '150' },
              { prop: 'address', label: '地址', width: '150' },
              { prop: 'tel', label: '电话', width: '100' },
              { prop: 'openingBank', label: '开户行', width: '200' },
              { prop: 'account', label: '账号', width: '200' }
            ], component: configs
          },
          { prop: 'remark', label: '备注说明', width: '160' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增合同协议',
        dialogType: 0,
        teamRow: {
          type: 1
        },
        teamRowClear: {
          type: 1
        },
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {
          type: [
            { required: true, message: '请选择类型', trigger: 'change' }
          ],
          projectName: [
            { required: true, message: '请输入合同名称', trigger: 'change' }
          ],
          signDate: [
            { required: true, message: '请选择签订日期', trigger: 'change' }
          ],
          signUnit: [
            { required: true, message: '请输入签订单位', trigger: 'change' }
          ],
          invoiceName: [
            { required: true, message: '请输入开票名称', trigger: 'change' }
          ],
          ird: [
            { required: true, message: '请输入税号', trigger: 'change' }
          ]
        },
        visible: false,
        typeList: [
          { value: 1, label: '付款' },
          { value: 2, label: '到账' }
        ]
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        console.log(this.searchFormList)
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增合同协议'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(projectId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改合同协议'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          projectId: projectId
        }
        selectContractDeal(params).then(response => {
          const data = response.responseBody
          this.teamRow = Object.assign({}, this.teamRow, data)
        })
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          type: this.teamRow.type,
          projectContract: this.teamRow.projectContract || '',
          projectName: this.teamRow.projectName,
          signDate: this.teamRow.signDate,
          signUnit: this.teamRow.signUnit,
          projectFee: this.teamRow.projectFee || 0,
          oneDate: this.teamRow.oneDate,
          oneFee: this.teamRow.oneFee,
          twoDate: this.teamRow.twoDate,
          twoFee: this.teamRow.twoFee,
          threeDate: this.teamRow.threeDate,
          threeFee: this.teamRow.threeFee,
          fourDate: this.teamRow.fourDate,
          fourFee: this.teamRow.fourFee,
          fiveDate: this.teamRow.fiveDate,
          fiveFee: this.teamRow.fiveFee,
          sixDate: this.teamRow.sixDate,
          sixFee: this.teamRow.sixFee,
          invoiceName: this.teamRow.invoiceName,
          ird: this.teamRow.ird,
          address: this.teamRow.address || '',
          tel: this.teamRow.tel || '',
          openingBank: this.teamRow.openingBank || '',
          account: this.teamRow.account || '',
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.projectId = this.teamRow.projectId
          params.createdBy = this.teamRow.createdBy
          params.dateCreated = this.teamRow.dateCreated
          params.isDel = this.teamRow.isDel
          params.createdId = this.teamRow.createdId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addContractDeal(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateContractDeal(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          projectId: scope.row.projectId,
          isDel: 1
        }
        delContractDeal(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
